package com.example.tellwin.mine.presenter;

import com.example.tellwin.AppHelper;
import com.example.tellwin.MainApplication;
import com.example.tellwin.R;
import com.example.tellwin.api.TwjfApi;
import com.example.tellwin.base.RxPresenter;
import com.example.tellwin.base.SimpleCallback;
import com.example.tellwin.mine.bean.ExpenseRecordContentBean;
import com.example.tellwin.mine.bean.UserContentBean;
import com.example.tellwin.mine.contract.WalletContract;
import com.example.tellwin.utils.ToastUtil;

import javax.inject.Inject;

/**
 * @author : LinZePeng
 * @date : 2020/12/1
 * @Describe ：
 **/
public class WalletPresenter extends RxPresenter<WalletContract.View> implements WalletContract.Presenter<WalletContract.View> {

    private TwjfApi mApi;

    private int pages;
    private int pageNum;
    private final int pageSize = 10;
    private boolean isNotMore = true;

    @Inject
    public WalletPresenter(TwjfApi mApi) {
        this.mApi = mApi;
    }

    @Override
    public void getWalletDetail() {
        pageNum = 1;
        pages = 0;
        isNotMore = false;
        mApi.myAccount(pageNum + "", pageSize + "", new SimpleCallback<ExpenseRecordContentBean>(mView) {
            @Override
            public void onSuccess(ExpenseRecordContentBean expenseRecordContentBean) {
                pageNum = expenseRecordContentBean.getPageNum();
                pages = expenseRecordContentBean.getPages();
                if (expenseRecordContentBean.getMyAccountList() == null || expenseRecordContentBean.getMyAccountList().size() == 0) {
                 //   ToastUtil.show(MainApplication.getCurActivity(), R.string.none_data);
                    isNotMore = true;
                    return;
                }
                if(expenseRecordContentBean.getMyAccountList().size()<10){
                    isNotMore = true;
                }
                mView.walletDetailResult(expenseRecordContentBean.getMyAccountList());
            }

            @Override
            public void onFail(String msg) {

            }
        });
    }

    @Override
    public void loadMore() {
        if (isNotMore) {
            return;
        }
        if (pages != 0 && pageNum == pages) {
          //  ToastUtil.show(MainApplication.getCurActivity(), R.string.none_data);
            isNotMore = true;
            return;
        }
        pageNum++;
        mApi.myAccount(pageNum + "", pageSize + "", new SimpleCallback<ExpenseRecordContentBean>(mView) {
            @Override
            public void onSuccess(ExpenseRecordContentBean expenseRecordContentBean) {
                pageNum = expenseRecordContentBean.getPageNum();
                pages = expenseRecordContentBean.getPages();
                if (expenseRecordContentBean.getMyAccountList() == null || expenseRecordContentBean.getMyAccountList().size() == 0) {
                    ToastUtil.show(MainApplication.getCurActivity(), R.string.none_data);
                    isNotMore = true;
                    return;
                }
                mView.loadMoreResult(expenseRecordContentBean.getMyAccountList());
            }

            @Override
            public void onFail(String msg) {

            }
        });
    }

    @Override
    public void getUserInfo() {
        mApi.getUserInfo(new SimpleCallback<UserContentBean>(mView) {
            @Override
            public void onSuccess(UserContentBean userContentBean) {
                if(userContentBean==null ||userContentBean.getUserInfo()==null){
                    ToastUtil.show(MainApplication.getCurActivity() , R.string.none_data);
                }
                AppHelper.getInstance().setUserInfo(userContentBean.getUserInfo());
                mView.userInfoResult(userContentBean);
            }

            @Override
            public void onFail(String msg) {

            }
        });
    }
}
